Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bump candid #113

Merged
merged 3 commits into from
Apr 11, 2024
Merged

bump candid #113

merged 3 commits into from
Apr 11, 2024

Conversation

chenyan-dfinity
Copy link
Contributor

Test perf for dfinity/candid#542

Copy link

github-actions bot commented Apr 9, 2024

Note
Diffing the performance result against the published result from main branch.
Unchanged benchmarks are omitted.

Map

binary_size generate 1m max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
hashmap 189_929 8_184_618_025 56_000_256 342_784 6_462_528_122 368_420 10_728_193_099
triemap 195_472 13_661_315_924 68_228_576 252_649 657_794 648_084 15_499_470_884
rbtree 185_907 7_009_043_570 52_000_464 116_348 318_320 330_226 6_870_900_152
splay 190_457 13_157_617_583 48_000_400 631_329 662_998 928_144 4_308_925_798
btree 230_321 10_223_929_607 25_108_416 357_912 485_794 539_490 2_861_974_825
zhenya_hashmap 188_894 2_360_638_679 16_777_504 58_204 66_552 79_675 3_018_208_083
btreemap_rs 555_925 ($\textcolor{red}{0.41\%}$) 1_792_610_876 ($\textcolor{green}{-0.00\%}$) 27_590_656 73_295 ($\textcolor{green}{-2.81\%}$) 123_191 ($\textcolor{green}{-1.63\%}$) 85_043 ($\textcolor{green}{-1.98\%}$) 3_204_151_479 ($\textcolor{green}{-0.00\%}$)
imrc_hashmap_rs 556_123 ($\textcolor{red}{0.08\%}$) 2_584_010_729 ($\textcolor{green}{-0.02\%}$) 244_908_032 ($\textcolor{green}{-0.03\%}$) 35_545 ($\textcolor{green}{-6.51\%}$) 194_927 ($\textcolor{red}{8.79\%}$) 93_154 ($\textcolor{green}{-19.73\%}$) 6_272_274_871 ($\textcolor{red}{0.15\%}$)
hashmap_rs 544_570 ($\textcolor{red}{0.48\%}$) 439_245_801 ($\textcolor{green}{-0.90\%}$) 73_138_176 20_008 ($\textcolor{green}{-7.22\%}$) 24_930 ($\textcolor{green}{-6.45\%}$) 23_243 ($\textcolor{green}{-6.88\%}$) 1_565_611_027 ($\textcolor{green}{-0.00\%}$)

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50.1 upgrade
heap 166_903 5_554_617_018 24_000_360 621_690 227_224 592_588 3_189_831_485
heap_rs 540_668 ($\textcolor{red}{1.43\%}$) 139_667_629 ($\textcolor{green}{-0.00\%}$) 18_284_544 55_880 ($\textcolor{green}{-2.71\%}$) 21_271 ($\textcolor{green}{-7.48\%}$) 55_765 ($\textcolor{green}{-2.99\%}$) 648_921_342 ($\textcolor{green}{-0.00\%}$)

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 173_903 2_601_059 65_644 95_506 803_474 173_506 3_091_310
vector 171_932 1_952_689 24_580 126_130 186_485 176_123 4_675_192
vec_rs 538_516 ($\textcolor{red}{0.97\%}$) 286_776 ($\textcolor{green}{-0.91\%}$) 1_376_256 15_714 ($\textcolor{green}{-9.08\%}$) 28_793 ($\textcolor{green}{-5.67\%}$) 21_553 ($\textcolor{green}{-7.44\%}$) 3_805_750 ($\textcolor{green}{-0.55\%}$)

Stable structures

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
btreemap_rs 555_925 ($\textcolor{red}{0.41\%}$) 76_161_521 ($\textcolor{green}{-0.00\%}$) 2_555_904 62_853 ($\textcolor{green}{-3.26\%}$) 95_118 ($\textcolor{green}{-2.10\%}$) 84_048 ($\textcolor{green}{-2.00\%}$) 139_592_121 ($\textcolor{green}{-0.02\%}$)
btreemap_stable_rs 558_089 ($\textcolor{red}{0.48\%}$) 4_564_070_850 ($\textcolor{green}{-0.00\%}$) 2_031_616 2_709_647 ($\textcolor{red}{0.13\%}$) 5_031_627 ($\textcolor{green}{-0.05\%}$) 8_581_114 ($\textcolor{red}{0.03\%}$) 729_344 ($\textcolor{red}{0.00\%}$)
heap_rs 540_668 ($\textcolor{red}{1.43\%}$) 7_049_481 ($\textcolor{green}{-0.04\%}$) 2_293_760 48_389 ($\textcolor{green}{-3.12\%}$) 21_519 ($\textcolor{green}{-7.40\%}$) 48_114 ($\textcolor{green}{-3.45\%}$) 33_629_765 ($\textcolor{green}{-0.09\%}$)
heap_stable_rs 521_160 ($\textcolor{red}{0.65\%}$) 277_865_060 ($\textcolor{red}{2.04\%}$) 458_752 2_405_453 ($\textcolor{red}{4.28\%}$) 242_829 ($\textcolor{red}{1.47\%}$) 2_387_316 ($\textcolor{red}{4.27\%}$) 729_349
vec_rs 538_516 ($\textcolor{red}{0.97\%}$) 3_077_152 ($\textcolor{green}{-0.09\%}$) 2_293_760 15_714 ($\textcolor{green}{-9.08\%}$) 16_643 ($\textcolor{green}{-9.43\%}$) 15_941 ($\textcolor{green}{-9.80\%}$) 31_301_345 ($\textcolor{green}{-0.07\%}$)
vec_stable_rs 521_344 ($\textcolor{red}{1.18\%}$) 63_342_333 ($\textcolor{green}{-0.00\%}$) 458_752 64_876 ($\textcolor{red}{0.69\%}$) 78_806 ($\textcolor{green}{-1.16\%}$) 84_168 ($\textcolor{red}{0.65\%}$) 729_349 ($\textcolor{green}{-0.00\%}$)

Statistics

  • binary_size: 0.77% [0.52%, 1.02%]
  • max_mem: -0.03%
  • cycles: -2.22% [-3.23%, -1.20%]

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 193_686 267_743_355 247_834_501 33_636 24_532
Rust 541_887 ($\textcolor{red}{0.60\%}$) 82_781_801 ($\textcolor{green}{-0.01\%}$) 56_787_280 ($\textcolor{green}{-0.01\%}$) 41_044 ($\textcolor{green}{-14.41\%}$) 39_949 ($\textcolor{green}{-21.47\%}$)

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 243_641 4_666_119_661 3_430_044 553_629 407_936 274_434_719
Rust 580_473 ($\textcolor{red}{0.17\%}$) 6_409_375_828 ($\textcolor{green}{-0.00\%}$) 2_228_224 1_020_376 ($\textcolor{green}{-0.02\%}$) 298_204 ($\textcolor{green}{-2.16\%}$) 6_026_009_495 ($\textcolor{red}{0.00\%}$)

Statistics

  • binary_size: 0.38% [-0.97%, 1.73%]
  • max_mem: no change
  • cycles: -4.76% [-10.38%, 0.86%]

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal upgrade
Motoko 273_938 510_793 ($\textcolor{green}{-0.01\%}$) 22_382 ($\textcolor{red}{0.02\%}$) 18_539 ($\textcolor{green}{-0.31\%}$) 19_569 ($\textcolor{green}{-0.48\%}$) 157_946 ($\textcolor{green}{-0.01\%}$)
Rust 847_522 ($\textcolor{red}{0.06\%}$) 506_485 ($\textcolor{green}{-19.28\%}$) 88_938 ($\textcolor{green}{-11.75\%}$) 117_357 ($\textcolor{green}{-10.80\%}$) 112_347 ($\textcolor{green}{-19.37\%}$) 1_483_364 ($\textcolor{green}{-18.90\%}$)

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 220_403 481_158 30_204 8_764 89_833
Rust 879_920 ($\textcolor{red}{0.23\%}$) 203_509 ($\textcolor{green}{-16.30\%}$) 303_060 ($\textcolor{green}{-21.69\%}$) 71_233 ($\textcolor{green}{-21.57\%}$) 1_629_856 ($\textcolor{green}{-21.97\%}$)

Statistics

  • binary_size: 0.14% [-0.40%, 0.69%]
  • max_mem: no change
  • cycles: -11.60% [-16.07%, -7.13%]

Heartbeat

binary_size heartbeat
Motoko 137_183 19_507
Rust 23_657 480 ($\textcolor{green}{-56.83\%}$)

Timer

binary_size setTimer cancelTimer
Motoko 145_619 51_778 4_626
Rust 502_754 ($\textcolor{red}{1.02\%}$) 63_379 ($\textcolor{green}{-7.26\%}$) 11_676 ($\textcolor{green}{-0.19\%}$)

Statistics

  • binary_size: 1.02%
  • max_mem: no change
  • cycles: -3.72% [-26.04%, 18.59%]

Publisher & Subscriber

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 161_290 145_741 28_593 11_963 22_854 6_446
Rust 535_946 ($\textcolor{red}{0.29\%}$) 573_818 ($\textcolor{red}{0.16\%}$) 57_488 ($\textcolor{green}{-17.70\%}$) 37_798 ($\textcolor{green}{-13.71\%}$) 71_349 ($\textcolor{green}{-24.24\%}$) 42_449 ($\textcolor{green}{-20.43\%}$)

Statistics

  • binary_size: 0.22% [-0.20%, 0.64%]
  • max_mem: no change
  • cycles: -19.02% [-24.25%, -13.79%]

Overall Statistics

  • binary_size: 0.61% [0.43%, 0.79%]
  • max_mem: -0.03%
  • cycles: -4.92% [-6.31%, -3.54%]

Copy link

github-actions bot commented Apr 9, 2024

Note
The flamegraph link only works after you merge.
Unchanged benchmarks are omitted.

Collection libraries

Measure different collection libraries written in both Motoko and Rust.
The library names with _rs suffix are written in Rust; the rest are written in Motoko.
The _stable and _stable_rs suffix represents that the library directly writes the state to stable memory using Region in Motoko and ic-stable-stuctures in Rust.

We use the same random number generator with fixed seed to ensure that all collections contain
the same elements, and the queries are exactly the same. Below we explain the measurements of each column in the table:

  • generate 1m. Insert 1m Nat64 integers into the collection. For Motoko collections, it usually triggers the GC; the rest of the column are not likely to trigger GC.
  • max mem. For Motoko, it reports rts_max_heap_size after generate call; For Rust, it reports the Wasm's memory page * 32Kb.
  • batch_get 50. Find 50 elements from the collection.
  • batch_put 50. Insert 50 elements to the collection.
  • batch_remove 50. Remove 50 elements from the collection.
  • upgrade. Upgrade the canister with the same Wasm module. For non-stable benchmarks, the map state is persisted by serializing and deserializing states into stable memory. For stable benchmarks, the upgrade takes no cycles, as the state is already in the stable memory.

💎 Takeaways

  • The platform only charges for instruction count. Data structures which make use of caching and locality have no impact on the cost.
  • We have a limit on the maximal cycles per round. This means asymptotic behavior doesn't matter much. We care more about the performance up to a fixed N. In the extreme cases, you may see an $O(10000 n\log n)$ algorithm hitting the limit, while an $O(n^2)$ algorithm runs just fine.
  • Amortized algorithms/GC may need to be more eager to avoid hitting the cycle limit on a particular round.
  • Rust costs more cycles to process complicated Candid data, but it is more efficient in performing core computations.

Note

  • The Candid interface of the benchmark is minimal, therefore the serialization cost is negligible in this measurement.
  • Due to the instrumentation overhead and cycle limit, we cannot profile computations with very large collections.
  • The upgrade column uses Candid for serializing stable data. In Rust, you may get better cycle cost by using a different serialization format. Another slowdown in Rust is that ic-stable-structures tends to be slower than the region memory in Motoko.
  • Different library has different ways for persisting data during upgrades, there are mainly three categories:
    • Use stable variable directly in Motoko: zhenya_hashmap, btree, vector
    • Expose and serialize external state (share/unshare in Motoko, candid::Encode in Rust): rbtree, heap, btreemap_rs, hashmap_rs, heap_rs, vector_rs
    • Use pre/post-upgrade hooks to convert data into an array: hashmap, splay, triemap, buffer, imrc_hashmap_rs
  • The stable benchmarks are much more expensive than their non-stable counterpart, because the stable memory API is much more expensive. The benefit is that they get fast upgrade. The upgrade still needs to parse the metadata when initializing the upgraded Wasm module.
  • hashmap uses amortized data structure. When the initial capacity is reached, it has to copy the whole array, thus the cost of batch_put 50 is much higher than other data structures.
  • btree comes from mops.one/stableheapbtreemap.
  • zhenya_hashmap comes from mops.one/map.
  • vector comes from mops.one/vector. Compare with buffer, put has better worst case time and space complexity ($O(\sqrt{n})$ vs $O(n)$); get has a slightly larger constant overhead.
  • hashmap_rs uses the fxhash crate, which is the same as std::collections::HashMap, but with a deterministic hasher. This ensures reproducible result.
  • imrc_hashmap_rs uses the im-rc crate, which is the immutable version hashmap in Rust.

Map

binary_size generate 1m max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
hashmap 189_929 8_184_618_025 56_000_256 342_784 6_462_528_122 368_420 10_728_193_099
triemap 195_472 13_661_315_924 68_228_576 252_649 657_794 648_084 15_499_470_884
rbtree 185_907 7_009_043_570 52_000_464 116_348 318_320 330_226 6_870_900_152
splay 190_457 13_157_617_583 48_000_400 631_329 662_998 928_144 4_308_925_798
btree 230_321 10_223_929_607 25_108_416 357_912 485_794 539_490 2_861_974_825
zhenya_hashmap 188_894 2_360_638_679 16_777_504 58_204 66_552 79_675 3_018_208_083
btreemap_rs 555_925 1_792_610_876 27_590_656 73_295 123_191 85_043 3_204_151_479
imrc_hashmap_rs 556_123 2_584_010_729 244_908_032 35_545 194_927 93_154 6_272_274_871
hashmap_rs 544_570 439_245_801 73_138_176 20_008 24_930 23_243 1_565_611_027

Priority queue

binary_size heapify 1m max mem pop_min 50 put 50 pop_min 50 upgrade
heap 166_903 5_554_617_018 24_000_360 621_690 227_224 592_588 3_189_831_485
heap_rs 540_668 139_667_629 18_284_544 55_880 21_271 55_765 648_921_342

Growable array

binary_size generate 5k max mem batch_get 500 batch_put 500 batch_remove 500 upgrade
buffer 173_903 2_601_059 65_644 95_506 803_474 173_506 3_091_310
vector 171_932 1_952_689 24_580 126_130 186_485 176_123 4_675_192
vec_rs 538_516 286_776 1_376_256 15_714 28_793 21_553 3_805_750

Stable structures

binary_size generate 50k max mem batch_get 50 batch_put 50 batch_remove 50 upgrade
btreemap_rs 555_925 76_161_521 2_555_904 62_853 95_118 84_048 139_592_121
btreemap_stable_rs 558_089 4_564_070_850 2_031_616 2_709_647 5_031_627 8_581_114 729_344
heap_rs 540_668 7_049_481 2_293_760 48_389 21_519 48_114 33_629_765
heap_stable_rs 521_160 277_865_060 458_752 2_405_453 242_829 2_387_316 729_349
vec_rs 538_516 3_077_152 2_293_760 15_714 16_643 15_941 31_301_345
vec_stable_rs 521_344 63_342_333 458_752 64_876 78_806 84_168 729_349

Environment

  • dfx 0.18.0
  • Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
  • rustc 1.76.0 (07dca489a 2024-02-04)
  • ic-repl 0.7.0
  • ic-wasm 0.7.0

Cryptographic libraries

Measure different cryptographic libraries written in both Motoko and Rust.

  • SHA-2 benchmarks
    • SHA-256/SHA-512. Compute the hash of a 1M Wasm binary.
    • account_id. Compute the ledger account id from principal, based on SHA-224.
    • neuron_id. Compute the NNS neuron id from principal, based on SHA-256.
  • Certified map. Merkle Tree for storing key-value pairs and generate witness according to the IC Interface Specification.
    • generate 10k. Insert 10k 7-character word as both key and value into the certified map.
    • max mem. For Motoko, it reports rts_max_heap_size after generate call; For Rust, it reports the Wasm's memory page * 32Kb.
    • inc. Increment a counter and insert the counter value into the map.
    • witness. Generate the root hash and a witness for the counter.
    • upgrade. Upgrade the canister with the same Wasm. In Motoko, we use stable variable. In Rust, we convert the tree to a vector before serialization.

SHA-2

binary_size SHA-256 SHA-512 account_id neuron_id
Motoko 193_686 267_743_355 247_834_501 33_636 24_532
Rust 541_887 82_781_801 56_787_280 41_044 39_949

Certified map

binary_size generate 10k max mem inc witness upgrade
Motoko 243_641 4_666_119_661 3_430_044 553_629 407_936 274_434_719
Rust 580_473 6_409_375_828 2_228_224 1_020_376 298_204 6_026_009_495

Environment

  • dfx 0.18.0
  • Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
  • rustc 1.76.0 (07dca489a 2024-02-04)
  • ic-repl 0.7.0
  • ic-wasm 0.7.0

Sample Dapps

Measure the performance of some typical dapps:

  • Basic DAO,
    with heartbeat disabled to make profiling easier. We have a separate benchmark to measure heartbeat performance.
  • DIP721 NFT

Note

  • The cost difference is mainly due to the Candid serialization cost.
  • Motoko statically compiles/specializes the serialization code for each method, whereas in Rust, we use serde to dynamically deserialize data based on data on the wire.
  • We could improve the performance on the Rust side by using parser combinators. But it is a challenge to maintain the ergonomics provided by serde.
  • For real-world applications, we tend to send small data for each endpoint, which makes the Candid overhead in Rust tolerable.

Basic DAO

binary_size init transfer_token submit_proposal vote_proposal upgrade
Motoko 273_938 510_793 22_382 18_539 19_569 157_946
Rust 847_522 506_485 88_938 117_357 112_347 1_483_364

DIP721 NFT

binary_size init mint_token transfer_token upgrade
Motoko 220_403 481_158 30_204 8_764 89_833
Rust 879_920 203_509 303_060 71_233 1_629_856

Environment

  • dfx 0.18.0
  • Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
  • rustc 1.76.0 (07dca489a 2024-02-04)
  • ic-repl 0.7.0
  • ic-wasm 0.7.0

Heartbeat / Timer

Measure the cost of empty heartbeat and timer job.

  • setTimer measures both the setTimer(0) method and the execution of empty job.
  • It is not easy to reliably capture the above events in one flamegraph, as the implementation detail
    of the replica can affect how we measure this. Typically, a correct flamegraph contains both setTimer and canister_global_timer function. If it's not there, we may need to adjust the script.

Heartbeat

binary_size heartbeat
Motoko 137_183 19_507
Rust 23_657 480

Timer

binary_size setTimer cancelTimer
Motoko 145_619 51_778 4_626
Rust 502_754 63_379 11_676

Environment

  • dfx 0.18.0
  • Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
  • rustc 1.76.0 (07dca489a 2024-02-04)
  • ic-repl 0.7.0
  • ic-wasm 0.7.0

Publisher & Subscriber

Measure the cost of inter-canister calls from the Publisher & Subscriber example.

pub_binary_size sub_binary_size subscribe_caller subscribe_callee publish_caller publish_callee
Motoko 161_290 145_741 28_593 11_963 22_854 6_446
Rust 535_946 573_818 57_488 37_798 71_349 42_449

Environment

  • dfx 0.18.0
  • Motoko compiler 0.11.0 (source lndfxrzc-zr7pf1k6-nr3nr3d7-jfla8nbn)
  • rustc 1.76.0 (07dca489a 2024-02-04)
  • ic-repl 0.7.0
  • ic-wasm 0.7.0

@chenyan-dfinity chenyan-dfinity marked this pull request as ready for review April 11, 2024 20:01
@chenyan-dfinity chenyan-dfinity merged commit e87dd0d into main Apr 11, 2024
3 checks passed
@chenyan-dfinity chenyan-dfinity deleted the bump-candid branch April 11, 2024 20:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants